package com.zhht.checkcop.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.zhbc.framework.support.Result;
import com.zhbc.framework.support.Transform;
import com.zhbc.framework.utils.StringUtils;
import com.zhht.checkcop.check.vo.MovieVO;
import com.zhht.checkcop.service.MovieService;

@Controller
public class MovieCotroller {

	@Autowired
	private MovieService movieService;

	private final static Logger log = Logger.getLogger(MovieCotroller.class);

	@RequestMapping(value = "/movieapp/list", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String chapter(@RequestParam String param, HttpServletRequest request, HttpServletResponse re) {
		Result r = new Result();
		Transform<Map<String, Object>> p = Transform.fromJson(param, Map.class);
		try {
			Map<String, Object> data = p.getData();
			if(data == null){
				log.info("请求参数为null   param："+param);
				return r.failure("请求参数为null");
			}
			if(StringUtils.isStrTrimNull(data.get("classifyId")+"")){
				return r.failure("请求参数classifyId不存在");
			}
			if(StringUtils.isStrTrimNull(data.get("sort")+"")){
				return r.failure("请求参数sort不存在");
			}
			
			if(StringUtils.isStrTrimNull(data.get("accessKey")+"")){
				return r.failure("请求参数accessKey不存在");
			}
			PageInfo<MovieVO> movieVO = movieService.getListByClassify(data.get("classifyId")+"",data.get("sort")+"",p.getPageNum(),p.getPageSize());
			String json=JSON.toJSONString(movieVO.getList());
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("list", json);
			r.setData(map);
			r.setTotal(movieVO.getTotal());
			
		} catch (Exception e) {
			log.info("查询异常："+e);
			return r.failure("请求失败");
		}
		return r.success();
	}
	
	@RequestMapping(value = "/movieapp/search", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String search(@RequestParam String param, HttpServletRequest request, HttpServletResponse re) {
		Result r = new Result();
		Transform<Map<String, Object>> p = Transform.fromJson(param, Map.class);
		Map<String, Object> data = p.getData();
		try {
			if(data == null){
				log.info("请求参数为null   param："+param);
				return r.failure("请求参数为null");
			}
			if(StringUtils.isStrTrimNull(data.get("type")+"")){
				return r.failure("请求参数type不存在");
			}
			if(StringUtils.isStrTrimNull(data.get("word")+"")){
				return r.failure("请求参数word不存在");
			}
			if(StringUtils.isStrTrimNull(data.get("accessKey")+"")){
				return r.failure("请求参数accessKey不存在");
			}
			
			String MovieName = data.get("word").toString();
			
			List<MovieVO> list = movieService.getListByName(MovieName, p.getPageNum(), p.getPageSize());
			String json=JSON.toJSONString(list);
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("movie", json);
			
			r.setData(map);
			r.setTotal(list.size());
		} catch (Exception e) {
			log.info("查询异常："+e);
			return r.failure("请求失败");
		}
		return r.success();
	}
	
	@RequestMapping(value = "/movieapp/info", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String category_Movie(@RequestParam String param, HttpServletRequest request, HttpServletResponse re) {
		Result r = new Result();
		Transform<Map<String, Object>> p = Transform.fromJson(param, Map.class);
		Map<String, Object> data = p.getData();
		try {
			if(data == null){
				log.info("请求参数为null   param："+param);
				return r.failure("请求参数为null");
			}
			if(StringUtils.isStrTrimNull(data.get("accessKey")+"")){
				return r.failure("请求参数accessKey不存在");
			}
			if(StringUtils.isStrTrimNull(data.get("movieId")+"")){
				return r.failure("请求参数movieId不存在");
			}
			String movieId = data.get("movieId").toString();
			MovieVO movieVO = movieService.getMovieVOById(movieId);
			if(movieVO!=null){
				List<String> picList = movieService.getMoviePicUrl(movieId);
				if(picList!=null && picList.size()>0){
					movieVO.setPicUrl(picList);
				}
				String json=JSON.toJSONString(movieVO);
				Map<String,Object> map = new HashMap<String, Object>();
				map.put("movie", json);
				r.setData(map);
			}
		} catch (Exception e) {
			log.info("查询异常："+e);
			return r.failure("请求失败");
		}
		return r.success();
	}
}
